package com.authine.cloudpivot.ext.applicationservice;

import com.alibaba.cola.dto.SingleResponse;
import com.alibaba.fastjson.JSONObject;
import com.authine.cloudpivot.ext.applicationservice.base.ApplicationBaseService;
import com.authine.cloudpivot.ext.constants.AppConst;
import com.authine.cloudpivot.ext.constants.ModelConst;
import com.authine.cloudpivot.ext.dto.DeliverNoticeVO;
import com.authine.cloudpivot.ext.dto.DeliverVO;
import com.authine.cloudpivot.ext.utils.BoResultFormat;
import com.authine.cloudpivot.ext.utils.JdbcTemplateUtils;
import com.authine.cloudpivot.ext.utils.StringUtils;
import com.authine.mvp.app.launcher.domain.utills.ApplicationContextUtils;
import com.authine.mvp.app.launcher.dto.bo.response.BO;
import com.authine.mvp.app.launcher.dto.bo.response.BOList;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import java.util.List;
import java.util.Map;

import static com.authine.hermes.app.launcher.service.ApplicationServiceFoundation.CUSTOM_SERVICE;

/**
 * @ClassName VmiDeliverNoticeService
 * @Description: 寄售发货通知自定义服务
 * @Author cjj
 * @Date 2021/4/20
 * @Version V1.0
 **/
@Path("VmiDeliverNoticeService")
@Slf4j
public class VmiDeliverNoticeService extends ApplicationBaseService {
    private NamedParameterJdbcTemplate jdbcTemplate = ApplicationContextUtils.getBean(NamedParameterJdbcTemplate.class);

    /**
     * 寄售发货通知-全部-发货通知单详情/寄售发货通知-代发货-发货通知单详情
     *
     * @param dto
     * @return
     */
    @Path("deliverNoticeDetail")
    @POST
    @Operation(summary = "寄售发货通知-全部-发货通知单详情/寄售发货通知-代发货-发货通知单详情", tags = CUSTOM_SERVICE, description = "id必填")
    public SingleResponse<BOList> deliverNoticeDetail(@Parameter DeliverNoticeVO dto) {
        if (StringUtils.isBlank(dto.getId())) {
            return SingleResponse.buildFailure("-1", "发货通知单ID必填");
        }
        List<Map<String, Object>> resultList = JdbcTemplateUtils.builder(AppConst.COMMODITYMATERIALS, ModelConst.T_MATERIAL_NOTICE).selectField("*").eq("deliver_notice_id", dto.getId())
                .eq("del", false).queryForList();
        log.info("------------------------寄售发货通知-全部-发货通知单详情/寄售发货通知-代发货-发货通知单详情 deliverNoticeDetail Result：{}------------------------", JSONObject.toJSONString(resultList));
        //构建结果集
        BoResultFormat.resultFormat(resultList, ModelConst.T_MATERIAL_NOTICE);
        BOList bOList = new BOList();
        bOList.setData(resultList);
        bOList.setTotal(resultList.size());
        bOList.setSchemaCode(ModelConst.T_MATERIAL_NOTICE);
        return SingleResponse.of(bOList);
    }

    /**
     * 寄售发货通知-待入库-发货单详情
     *
     * @param dto
     * @return
     */
    @Path("deliverDetail")
    @POST
    @Operation(summary = "寄售发货通知-待入库-发货单详情", tags = CUSTOM_SERVICE, description = "id必填")
    public SingleResponse<BO> deliverDetail(@Parameter DeliverVO dto) {
        if (StringUtils.isBlank(dto.getId())) {
            return SingleResponse.buildFailure("-1", "发货单ID必填");
        }
        Map<String, Object> deliverMap = JdbcTemplateUtils.builder(ModelConst.T_DELIVER).selectField("*")
                .eq("id", dto.getId()).eq("del", false).queryForMap();
        if (null == deliverMap || deliverMap.isEmpty()) {
            return SingleResponse.buildFailure("-1", "发货通知单不存在");
        }
        List<Map<String, Object>> deliverNoticeList = JdbcTemplateUtils.builder(AppConst.COMMODITYMATERIALS, ModelConst.T_DELIVER_NOTICE).selectField("*")
                .eq("id", deliverMap.get("deliver_notice_id")).eq("del", false).queryForList();
        List<Map<String, Object>> materialList = JdbcTemplateUtils.builder(ModelConst.T_MATERIAL).selectField("*")
                .eq("deliver_id", dto.getId()).eq("del", false).queryForList();
        deliverMap.put("deliverNoticeList", deliverNoticeList);
        deliverMap.put("materialList", materialList);
        //构建结果集
        BO bo = new BO();
        bo.setData(deliverMap);
        bo.setSchemaCode(ModelConst.T_DELIVER);
        return SingleResponse.of(bo);
    }

    /**
     * 寄售发货通知-全部-修改发货通知单页-修改发货通知单
     *
     * @param dto
     * @return
     */
    @Path("updateDeilverNotice")
    @POST
    @Operation(summary = "寄售发货通知-全部-修改发货通知单页接口", tags = CUSTOM_SERVICE, description = "id必填")
    public SingleResponse<BO> updateDeilverNotice(@Parameter DeliverNoticeVO dto) {
        return updateDeliveryNotice(dto, false);
    }

    /**
     * 寄售发货通知-全部-修改发货通知单页-修改并提交发货通知单
     *
     * @param dto
     * @return
     */
    @Path("updateAndSubmitDeilverNotice")
    @POST
    @Operation(summary = "寄售发货通知-全部-修改并提交发货通知单", tags = CUSTOM_SERVICE, description = "id必填")
    public SingleResponse<BO> updateAndSubmitDeilverNotice(@Parameter DeliverNoticeVO dto) {
        return updateDeliveryNotice(dto, true);
    }

    /**
     * 寄售发货通知-全部-提交
     *
     * @param dto
     * @return
     */
    @Path("submitDeilverNotice")
    @POST
    @Operation(summary = "寄售发货通知-全部-提交", tags = CUSTOM_SERVICE, description = "ids必填")
    public SingleResponse<BO> submitDeilverNotice(@Parameter DeliverNoticeVO dto) {
        submitDeliveryNotice(dto.getIds());
        return SingleResponse.of(new BO());

    }

    /**
     * 寄售发货通知-全部-取消
     *
     * @param dto
     * @return
     */
    @Path("cancleDeilverNotice")
    @POST
    @Operation(summary = "寄售发货通知-全部-取消", tags = CUSTOM_SERVICE, description = "ids必填")
    public SingleResponse<BO> cancleDeilverNotice(@Parameter DeliverNoticeVO dto) {
        cancelDeliveryNotice(dto.getIds());
        return SingleResponse.of(new BO());
    }

    /**
     * 寄售发货通知-代发货-创建发货单-提交发货单
     *
     * @param dto
     * @return
     */
    @Path("saveAndSubmitDeilver")
    @POST
    @Operation(summary = "寄售发货通知-代发货-创建发货单-提交发货单", tags = CUSTOM_SERVICE, description = "customerId、vendorId、deliverNoticeId、waybill、logistics、" +
            "materialVOS.goodsId、materialVOS.brand、materialVOS.name、materialVOS.model、materialVOS.unit、materialVOS.pretaxPrice、materialVOS.price、" +
            "materialVOS.num、materialVOS.spec、materialVOS.taxRate必填")
    public SingleResponse<BO> saveAndSubmitDeilver(@Parameter DeliverVO dto) {
        return saveDelivery(dto, true);
    }
}
